﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using project.UI;
using project.UI.sdudent;
using Sunny.UI;
namespace project
{
    public partial class chose_wnd : UIForm
    {
       private string LoginCAPTCHA;

        public chose_wnd()
        {
            InitializeComponent();
        }
        private void UpdateCAPTCHA()
        {
            LoginCAPTCHA = Guid.NewGuid().ToString().Substring(0, 5).ToLower();
            LinkLabel_CAPTCHA.Text = LoginCAPTCHA;
        }

        private void chose_wnd_Load(object sender, EventArgs e)
        {
            UpdateCAPTCHA();

        }

        private void uiButton3_Click(object sender, EventArgs e)
        {

        }

        private void uiButton_admin_login_Click(object sender, EventArgs e)
        {
            // 管理员登录
            string admin_id=uiTextBox1.Text.Trim();
            string admin_pwd=uiTextBox2.Text.Trim();
            string captcha = uiTextBox3.Text.Trim();
            if(string.IsNullOrEmpty(admin_pwd)|| string.IsNullOrEmpty(admin_id))
            {
                MessageBox.Show("输入了无效的账号或者密码,请重新输入", "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                
            }
            if (string.IsNullOrEmpty(captcha))
            {
                MessageBox.Show("请输入验证码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                UpdateCAPTCHA();
                return;
            }

            if (!captcha.Equals(LoginCAPTCHA))
            {
                MessageBox.Show("验证码输入错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                UpdateCAPTCHA();
                return;
            }


            if (!SqlHelper.is_admin_pwd_ok(admin_id, admin_pwd))
            {
                MessageBox.Show("密码错误或管理员不存在", "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                LoginHelper.is_admin = true;
                LoginHelper.whoami = admin_id;
                admin_ui a = new admin_ui();
                this.Hide();
                a.ShowDialog();

                
            }
        }

        private void uiTextBox1_MouseClick(object sender, MouseEventArgs e)
        {
            this.Text = null;
        }

        private void uiTextBox2_MouseClick(object sender, MouseEventArgs e)
        {
            this.Text = null;
        }

        private void uiTextBox1_MouseDown(object sender, MouseEventArgs e)
        {
            this.Text = null;
        }

        private void uiLabel4_Click(object sender, EventArgs e)
        {
            // 找回密码
        }

        private void uiLabel3_Click(object sender, EventArgs e)
        {
            // 注册
            FormSignUp form = new FormSignUp();
            form.ShowDialog();
        }

        private void uiButton_student_login_Click(object sender, EventArgs e)
        {
            // 学生登录
            string id=uiTextBox1.Text.Trim();
            string pwd=uiTextBox2.Text.Trim();
            string captcha = uiTextBox3.Text.Trim();
            if(string.IsNullOrEmpty(pwd)|| string.IsNullOrEmpty(id))
            {
                MessageBox.Show("输入了无效的账号或者密码,请重新输入", "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                
            }
            if (string.IsNullOrEmpty(captcha))
            {
                MessageBox.Show("请输入验证码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                UpdateCAPTCHA();
                return;
            }

            if (!captcha.Equals(LoginCAPTCHA))
            {
                MessageBox.Show("验证码输入错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                UpdateCAPTCHA();
                return;
            }

            if (!SqlHelper.is_user_pwd_ok(id, pwd))
            {
                MessageBox.Show("密码错误或用户存在", "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                LoginHelper.is_admin = false;
                LoginHelper.whoami = id;
                StudentForm a = new StudentForm();
                this.Hide();
                a.ShowDialog();

                
            }
        }

        private void uiLinkLabel1_Click(object sender, EventArgs e)
        {
            // 注册
            FormFindPwd form = new FormFindPwd();
            form.ShowDialog();
        }

        private void uiLabel2_Click(object sender, EventArgs e)
        {

        }

        private void uiTextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiLabel1_Click(object sender, EventArgs e)
        {

        }

        private void uiTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void LinkLabel_CAPTCHA_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            UpdateCAPTCHA();

        }
    }
}
